/**
 * 获取登录code
 * @returns  {Promise<Boolean>}
 */
const getCode = () => {
	return new Promise((resolve) => {
		uni.login({
			success: (res) => {
				if (res.errMsg === 'login:ok' && res.code) {
					resolve(res.code);
				} else {
					resolve(false);
				}
			},
			fail: () => {
				resolve(false);
			}
		})
	})
}

/**
 * 判断登录
 * @param {string} success 回调方法
 * @returns  {Promise<Boolean>}
 */
const isLogin = async (success = '') => {
	return new Promise(async (resolve) => {
		if (wx.getStorageSync('userInfo')) {
			console.log('isLogin-已经登陆')
			if (success) success();
			resolve();
		} else {
			console.log('isLogin-未经登陆')
			uni.navigateTo({
				url: '/pages/login_userInfo/login_userInfo'
			})
		}
	})
}

const isPhone = async (success = '') => {
	return new Promise(async (resolve) => {
		if (wx.getStorageSync('phone')) {
			console.log('已有手机号')
			if (success) success();
			resolve();
		} else {
			console.log('未找到手机号')
			uni.navigateTo({
				url: '/pages/login_phone/login_phone'
			})
		}
	})
}


module.exports = {
	getCode, // 获取登录code
	isLogin, // 验证登录状态
	isPhone,
}
